//
//  WenZhangXuanXiangViewController.swift
//  CKC
//
//  Created by 张振东 on 16/10/2.
//  Copyright © 2016年 ZZD. All rights reserved.
//

import UIKit

protocol WenZhangSheZhiViewControllerDelegate:class {
    func chuanDiWenZhangMoShiAndFenZhongAndActionSheet(_ xuanZhongDeMoShiZhi:String,xuanZhongFenZhongZhi:Int,actionSheetIndex:Int)
}

class WenZhangXuanXiangViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,ActionViewDelegate {

    weak var delegate:WenZhangSheZhiViewControllerDelegate?
    
    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    
    var moShiLeiBieTableView:UITableView?
    
    var moShiItems:[String] = ["计时模式","定时模式"]
    var segmentArray:[String] = ["5","10","15"]
    
    //定义一个临时变量存储当switch开启和关闭时,代表的模式
    var linShiMoShiChangedValue:String?
    
    //定义一个练习时长label对象
    var xunLianShiChang:UILabel?
    
    //定义一个训练时长segment对象
    var segmentController:UISegmentedControl?
    
    //定义一个完成按钮
    var wanChengBtn:UIButton?
    
    //定义一个变量,用于保存segment选中的值
    var selectedSegmentValue:Int = 1
    
    //创建一个按钮"点我啊"
    var clickeBtw:UIButton?
    
    var actionView :ActionView?
    
    //定义一个变量,保存选中的actionSheet下标
    var actionSheetIndex:Int = 0

    override func viewDidLoad() {
        self.title = "设置"
        self.view.backgroundColor = UIColor(red: 232/255.0,green:232/255.0,blue:232/255.0,alpha:1)
        let leftBarButton = UIButton(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
        leftBarButton.setBackgroundImage(UIImage(named: "fanhui"), for: UIControlState())
        leftBarButton.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarButtonItem = UIBarButtonItem(customView: leftBarButton)
        self.navigationItem.leftBarButtonItems = [leftBarButtonItem]
        
        
        //初始化UITableView
        moShiLeiBieTableView = UITableView(frame:self.view.frame,style: .grouped)
        //设置代理和数据源
        self.moShiLeiBieTableView?.delegate = self
        self.moShiLeiBieTableView?.dataSource = self
        self.view.addSubview(moShiLeiBieTableView!)
        let cellNib = UINib(nibName: "CiZuSheZhiTableViewCell", bundle: nil)
        self.moShiLeiBieTableView!.register(cellNib, forCellReuseIdentifier: "ciZuMoShiLeiBieTableViewCell")
        
        //初始化练习时长对象
        xunLianShiChang = UILabel(frame:CGRect(x: 15,y: 140,width: 80,height: 30))
        xunLianShiChang?.text = "训练时长:"
        xunLianShiChang?.font = UIFont.systemFont(ofSize: 15)
        xunLianShiChang?.textAlignment = .left
        xunLianShiChang?.clipsToBounds = true
        moShiLeiBieTableView?.addSubview(xunLianShiChang!)
        
        //初始化segmentController对象
        segmentController = UISegmentedControl(items:segmentArray)
        segmentController?.frame = CGRect(x: 15+80, y: 140, width: screenWidth-110, height: 30)
        segmentController?.selectedSegmentIndex = 0
        segmentController?.addTarget(self, action: #selector(segmentChange), for: .valueChanged)
        moShiLeiBieTableView?.addSubview(segmentController!)
        
        
        //初始化wanChengBtn按钮
        wanChengBtn = UIButton(frame: CGRect(x: 10, y: screenHeight-120, width: screenWidth-20 , height: 40))
        wanChengBtn?.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
        wanChengBtn?.layer.cornerRadius = 10
        wanChengBtn?.setTitle("完成", for: UIControlState())
        wanChengBtn?.addTarget(self, action: #selector(wanChengSheZhi), for: .touchUpInside)
        moShiLeiBieTableView?.addSubview(wanChengBtn!)
        
        
        //初始化clickeBtw
        clickeBtw = UIButton(frame:CGRect(x: 15, y: screenHeight/3,width: screenWidth-40, height: 40))
        clickeBtw?.setTitle("点我选择文章", for: UIControlState())
        clickeBtw?.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
        
        clickeBtw?.setTitleColor(UIColor.white, for: UIControlState())
        clickeBtw?.layer.cornerRadius = 5
        clickeBtw?.clipsToBounds = true
        
        clickeBtw?.addTarget(self, action: #selector(clickeBtwDidClicked), for: .touchUpInside)
        moShiLeiBieTableView?.addSubview(clickeBtw!)
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    func clickeBtwDidClicked(){
        self.actionView = ActionView(view:self.view,title:"标题",items: ["苏州","火-巴金","中国画","奥运会五环标志","云水苍茫说三山","全民健身计划纲要"],target:self,itemHeight: 60);
        self.actionView!.show();
    }
    
    //获得点击的下标
    func itemClicked(_ index: Int) {
        print(index)
        actionSheetIndex = index
        switch actionSheetIndex {
        case 0:
            clickeBtw?.setTitle("苏州", for: UIControlState())
            break
        case 1:
            clickeBtw?.setTitle("火-巴金", for: UIControlState())
            break
        case 2:
            clickeBtw?.setTitle("中国画", for: UIControlState())
            break
        case 3:
            clickeBtw?.setTitle("奥运会五环标志", for: UIControlState())
            break
        case 4:
            clickeBtw?.setTitle("云水苍茫说三山", for: UIControlState())
            break
        case 5:
            clickeBtw?.setTitle("全民健身计划纲要", for: UIControlState())
            break

        default:
            print("点击了取消")
            break
        }
    }
    
    /*
     作者:张振东
     时间:2016-9-28
     功能:返回几组
     */
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    /*
     作者:张振东
     时间:2016-9-28
     功能:每组显示几个cell
     */
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return moShiItems.count
    }
    
    /*
     作者:张振东
     时间:2016-9-28
     功能:定义tableViewcell的高度
     */
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    /*
     作者:张振东
     时间:2016-9-28
     功能:行高
     */
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20;
    }
    
    /*
     作者:张振东
     时间:2016-9-28
     功能:尾高
     */
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 20;
    }
    
    
    /*
     作者:张振东
     时间:2016-9-24
     功能:显示行内容
     */
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view:UIView = UIView(frame:CGRect.zero)
        let label = UILabel(frame: CGRect(x: 10, y: 0, width: screenWidth, height: 20))
        label.textColor = UIColor(red: 0x8f/255.0,green:0x8f/255.0,blue:0x94/255.0,alpha:1.0)
        label.font = UIFont.systemFont(ofSize: 16)
        view.addSubview(label)
        
        return view
    }
    
    /*
     作者:张振东
     时间:2016-9-24
     功能:显示尾部内容
     */
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view:UIView = UIView(frame:CGRect.zero)
        let label = UILabel(frame: CGRect(x: 10, y: 0, width: screenWidth, height: 20))
        label.textColor = UIColor(red: 0x8f/255.0,green:0x8f/255.0,blue:0x94/255.0,alpha:1.0)
        label.backgroundColor = UIColor.clear
        view.addSubview(label)
        return view
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: CiZuSheZhiTableViewCell = tableView.dequeueReusableCell(withIdentifier: "ciZuMoShiLeiBieTableViewCell") as! CiZuSheZhiTableViewCell!
        
        if indexPath.row == 0{
            cell.iconImageView?.image = UIImage(named: "jishi")
            cell.moShiSwitcher?.isOn = true
            cell.moShiSwitcher?.tag = 0
            linShiMoShiChangedValue = "计时"
            
        }else{
            cell.iconImageView?.image = UIImage(named: "dingshi")
            cell.moShiSwitcher?.isOn = false
            cell.moShiSwitcher?.tag = 1
            
        }
        cell.moShiLeiBie?.text = moShiItems[indexPath.row]
        cell.moShiSwitcher?.addTarget(self, action: #selector(switchChange), for: .valueChanged)
        return cell
        
    }



    /*
     作者:张振东
     时间:2016-9-28
     功能:当segment发生改变时,记录选中的值
     */
    func segmentChange(_ sender:Any){
        let segment = sender as! UISegmentedControl
        let selectedSegmentIndex = segment.selectedSegmentIndex
        print(selectedSegmentIndex)
        selectedSegmentValue = Int(segmentArray[selectedSegmentIndex])!
    }
    
    /*
     作者:张振东
     时间:2016-9-28
     功能:将选中的模式值和训练时长传递给上一个界面
     */
    func wanChengSheZhi(){
        print(linShiMoShiChangedValue as Any)
        print(selectedSegmentValue)
        //将上面的两个值通过协议的方式传值
        delegate?.chuanDiWenZhangMoShiAndFenZhongAndActionSheet(linShiMoShiChangedValue!, xuanZhongFenZhongZhi: selectedSegmentValue, actionSheetIndex: actionSheetIndex)
        self.navigationController?.popViewController(animated: true)
    }
    
    
    /*
     作者:张振东
     时间:2016-9-24
     功能:当UISwitch发生改变时,记录选中的值
     */
    func switchChange(_ sender:Any){
        let changedSwitch = sender as! UISwitch
        switch changedSwitch.tag  {
        case 0:
            if changedSwitch.isOn{
                linShiMoShiChangedValue = "计时"
            }
            break
            
        case 1:
            if changedSwitch.isOn{
                linShiMoShiChangedValue = "定时"
            }
            break
        default:
            print("")
        }
    }
    
    
    //返回到上一个页面
    func backToPrevious(){
        self.navigationController?.popViewController(animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
